/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Car() {
    this.javaClass = "limousine.vo.Car";
    
    this.carId ="";
    this.carName ="";
    this.carNo ="";
    this.carType ="";
    this.status ="";
    this.createBy ="";
    this.createDate ="";
    this.updateBy ="";
    this.updateDate ="";
   
}
function openLookup() {
    var body = $d("_body");
    body.innerHTML = requestPage("lookup.main.jsp");

    var x = new Driver();

    x.tableId
}
function edit(id) {
    ///alert("id["+ id +"]");
    try {        
        formEntry();
        document.getElementById("fieldSetLabel").textContent="Car Edit";
        var o = service.findById(id);
        //alert("caris["+ o.carId +"]");
        $d("carId").value = o.carId;
        $d("carName").value = o.carName;
        $d("carNo").value = o.carNo;
        $d("carType").value = o.carType;
        $d("status").value = o.status;
        $d("createBy").value = o.createBy;
        //$d("createDate").value = o.createDate;
        //$d("updateBy").value = o.updateBy;
        //$d("updateDate").value = o.updateDate;
        $d("bSave").setAttribute("onclick","save()");
        
        getLookupList("CAR_TYPE","carType",o.carType);
        getLookupList("STATUS","status",o.status);
    } catch(err) {
        alert("edit err:"+err);
    }
}

function switchProcess(type,msg,method){
    if(type == 0){//fail
        addSysMsg(0,msg);
    }else{
        addProcessing(msg);
        setTimeout(method,2000);        
    }    
}
function save() { 
    
    try {
        var o = new Car();        
        o.carId = $d("carId").value;
        o.carName = $d("carName").value;
        o.carNo = $d("carNo").value;
        o.carType = parseInt($d("carType").value);
        o.status = parseInt($d("status").value);
        o.createBy = $d("createBy").value;
        o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        o.updateDate = $d("updateDate").value;
        if( verify() == true ) {
            addProcessing('Please wait...');
            var ret = service.update(o);
            if( ret == "true" ) {     
                search(0);//refresh data
                switchProcess(1,"Save completed.","formSearch()");
            } else {
                switchProcess(0,"Save fail.","");                
            }
        }
    } catch(err) {
        alert("save err:"+err);
    }
}
function remove(id) {
    try {
        //alert("id["+ id +"]");
        service.remove(id);
        search(0);
    } catch(err) {
        alert("remove err:"+err);
    }
}
function search(offset) {
    
    try {               
        addProcessing('Please wait...');
        var ls = service.search(offset,$d("tCarName").value);        
        var jTable = new JTable();
        jTable.setTableId("tableData");        
        jTable.setColumns(["Car Name", "Car No","Car Type","Status", "Update By", "Update Date"]);
        jTable.setFields(["carName", "carNo","carTypeName","statusName", "updateByName", "updateDate"]);
        jTable.setEditFunction("edit");
        jTable.setDeleteFunction("remove");
        jTable.setDataId("carId");
        jTable.setCount(ls.size());
        jTable.setOffset(offset);
        jTable.setDataPageSize(20);
        jTable.setPageLink("search");        
        jTable.setTableHeader();
        jTable.loadData(ls);        
        clearWarningMsg();
    } catch(err) {
        alert("search err:"+err);        
    }
    clearAllMsg();
}
function insert() { 
    
    try {
        var o = new Car();
        o.carId = $d("carId").value;
        o.carName = $d("carName").value;
        o.carNo = $d("carNo").value;
        o.carType = parseInt($d("carType").value);
        o.status = parseInt($d("status").value);
        o.createBy = $d("createBy").value;        
        //o.createDate = $d("createDate").value;
        o.updateBy = $d("updateBy").value;
        //o.updateDate = $d("updateDate").value;
        //        var re = service.save(lookup);
       if( verify() == true ) {
        addProcessing('Please wait...');
        var ret = service.save(o);
        if( ret == "true" ) {            
            switchProcess(1,"Add completed.","formSearch()");
        } else {            
            switchProcess(0,"Add fail.","");
        }
     }
    } catch(err) {
        alert("insert err:"+err);
    }
}
        
function setInsert() {    
    try {
        formEntry();
        var searchd = $d("carId").value;
        $d("actionForm").reset();
        //$d("carId").value = searchd;
        $d("carId").value = "";
        $d("bSave").setAttribute("onclick","insert()");
        getLookupList("CAR_TYPE","carType","");
        getLookupList("STATUS","status","");
    } catch(err) {
        alert("setInsert err:"+err);
    }
}
         
function formEntry() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Car Add";
    try {
        $d("formSearch").style.visibility = "hidden";
        var formEntry = $d("formEntry");
        formEntry.style.visibility = "visible";
        var panel = $d("panel");
        formEntry.style.top = panel.style.top;
        formEntry.style.left = panel.style.left;
    } catch(err) {
        alert("formEntry err:"+err);
    }
}
function formSearch() {
    clearAllMsg();
    document.getElementById("fieldSetLabel").textContent="Car Search";    
    try {
        $d("formEntry").style.visibility = "hidden";
        var search = $d("formSearch");
        search.style.visibility = "visible";
        var panel = $d("panel");
        search.style.top = panel.style.top;
        search.style.left = panel.style.left;
    } catch(err) {
        alert("formSearch err:"+err);
    }
}
function verify() {    
    try {
        var v = new Validation();
        v.addField("name='carId',label='carId',min=1,max=50,type='string'");
        v.addField("name='carName',label='Car Name',min=1,max=50,type='string'");
        v.addField("name='carNo',label='Car No',min=4,max=10,type='string'");
        v.addField("name='carType',label='Car Type',min=0,max=10,type='int'");
        v.addField("name='status',label='Status',min=0,max=10,type='int'");
        var str = v.execute();
        if ( str.length == 0 ) {
            return true;
        } else {
            addSysMsg(0,str);
            return false;
        }
    } catch(err) {
        alert(err);
    }
}
function getLookupList(lookupGroupId,propertieId,selectedValue) {    
    var list = serviceLookup.getLookupType(lookupGroupId);
    var i=0;
    for( i =0; i < list.size(); i++ ) {
        var txt = list.get(i).lookupName;
        var val = list.get(i).lookupValue;
        
        if(val==selectedValue){
            $d(propertieId).options[i] = new Option(txt, val, false, true);
        }else{
            $d(propertieId).options[i] = new Option(txt, val, false, false);
        }
    }   
}